package com.matriq.cloudmanager;

import java.util.List;

import com.matriq.cloudmanager.R;
import com.matriq.cloudmanager.entity.ClassInfo;
import com.matriq.cloudmanager.service.AccountService;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import net.lgpage.activity.BaseActivity;
import net.lgpage.http.SwipeRefeshAsyncTask;
import net.lgpage.util.Checker.StringUtils;
import net.lgpage.util.Cleaner;
import net.lgpage.util.IntentUtil;

public class ClassInfoActivity extends BaseActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_class_info);
		setViews();
		setEvents();
		loadData();
	}

	TextView tv[] = new TextView[8];
	LinearLayout ll[] = new LinearLayout[8];
	ScrollView sv;
	TextView tv_title;
	ImageButton ll_back;

	private void setViews() {
		tv[0] = (TextView) this.findViewById(R.id.tv0);
		tv[1] = (TextView) this.findViewById(R.id.tv1);
		tv[2] = (TextView) this.findViewById(R.id.tv2);
		tv[3] = (TextView) this.findViewById(R.id.tv3);
		tv[4] = (TextView) this.findViewById(R.id.tv4);
		tv[5] = (TextView) this.findViewById(R.id.tv5);
		tv[6] = (TextView) this.findViewById(R.id.tv6);
		tv[7] = (TextView) this.findViewById(R.id.tv7);

		ll[0] = (LinearLayout) this.findViewById(R.id.ll0);
		ll[1] = (LinearLayout) this.findViewById(R.id.ll1);
		ll[2] = (LinearLayout) this.findViewById(R.id.ll2);
		ll[3] = (LinearLayout) this.findViewById(R.id.ll3);
		ll[4] = (LinearLayout) this.findViewById(R.id.ll4);
		ll[5] = (LinearLayout) this.findViewById(R.id.ll5);
		ll[6] = (LinearLayout) this.findViewById(R.id.ll6);
		ll[7] = (LinearLayout) this.findViewById(R.id.ll7);
		sv = (ScrollView) this.findViewById(R.id.sv);
		tv_title = (TextView) this.findViewById(R.id.tv_title);
		ll_back = (ImageButton) this.findViewById(R.id.ll_back);
	}

	private void setEvents() {
		ll[5].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(obj!=null&&!StringUtils.isEmpty(obj.getMphone())){
					startActivity(IntentUtil.toCall( obj.getMphone()));
				}
			}
		});
		ll[6].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(obj!=null&&!StringUtils.isEmpty(obj.getPhone())){
					startActivity(IntentUtil.toCall( obj.getPhone()));
				}
			}
		});
	}

	ClassInfo obj;

	public void setData(ClassInfo obj) {
		this.obj = obj;
		tv[0].setText(obj.getClassname());
		tv[1].setText(obj.getTeacher());
		tv[2].setText(obj.getDepartment());
		tv[3].setText(obj.getJob());
		tv[4].setText(obj.getRoomNum());
		tv[5].setText(obj.getMphone());
		tv[5].setTextColor(0xff4582fe);
		tv[6].setText(obj.getPhone());
		tv[7].setText(obj.getMail());
	}

	private void loadData() {
		reloadObj();
	}

	ObjTask objTask;

	public void reloadObj() {
		if (objTask != null) {
			objTask.cancel(true);
		}
		objTask = new ObjTask(getActivity());
		objTask.execute();
	}

	private class ObjTask extends SwipeRefeshAsyncTask<Object> {

		public ObjTask(Context context) {
			super(context);
		}

		@Override
		public Object doInBackgroundX(String... params) throws Exception {
			return AccountService.getService().base_class();
		}

		@SuppressWarnings("unchecked")
		@Override
		public void onPostExecuteX(Object result) {
			if (result == null) {
				return;
			} else {
				List<ClassInfo> list = (List<ClassInfo>) result;
				if (list.size() > 0) {
					setData(list.get(0));
				}
			}
		}
	}

	@Override
	public void onDestroy() {
		Cleaner.cleanTask(objTask);
		super.onDestroy();
	}

}